/**
 * 
 */
package com.dhcc.utils;

/**
 * 转换类型
 * @author hibean@qq.com 2017年6月28日
 *
 */
public class __Convert{
	/**
	 * 转换数据类型
	 * @param source
	 * @param targetType
	 * @return
	 */
	public static <T> T changeType(Object source, Class<?> targetType){
		if(source==null){
			return null;
		}
		if(targetType.isInstance(source)){
			return (T)source;
		}
		if(targetType==int.class||targetType==Integer.class){
			return (T)(Integer)toInt(source);
		}
		if(targetType==short.class||targetType==Short.class){
			return (T)(Short)toShort(source);
		}
		if(targetType==float.class||targetType==Float.class){
			return (T)(Float)toFloat(source);
		}
		if(targetType==long.class||targetType==Long.class){
			return (T)(Long)toLong(source);
		}
		if(targetType==double.class||targetType==Double.class){
			return (T)(Double)toDouble(source);
		}
		if(targetType==byte.class||targetType==Byte.class){
			return (T)(Byte)toByte(source);
		}
		if(targetType==char.class||targetType==Character.class){
			return (T)(Character)toChar(source);
		}
		if(targetType==boolean.class||targetType==Boolean.class){
			return (T)(Boolean)toBoolean(source);
		}
		if(targetType==String.class){
			return source==null?null:(T)String.valueOf(source);
		}
		throw new RuntimeException("只支持基本类型转换,无法将 "+source+" 转换为 "+targetType+" 类型的对象");
	}
	public static int toInt(Object obj){ 
		if(obj == null){ 		
			throw new NullPointerException("convert null to int");
		}
		if(obj instanceof Number){
			return ((Number)obj).intValue();
		}
		if(obj instanceof Character){
			return toInt((Character)obj);
		}else if(obj instanceof Boolean){
			return toInt((Boolean)obj);
		}else if(obj instanceof String){
			return toInt((String)obj);
		}
		throw new ClassCastException(obj+" 不能转换为 int 类型");
	}
	public static int toInt(Boolean v){ return v.booleanValue()?1:0; }
	public static int toInt(Character v){ return (int)v.charValue(); }
	public static int toInt(Byte v){ return v.intValue(); }
	public static int toInt(Integer v){ return v.intValue(); }
	public static int toInt(Float v){ return v.intValue(); }
	public static int toInt(Short v){ return v.intValue(); }
	public static int toInt(Double v){ return v.intValue(); }
	public static int toInt(String v){ return Integer.parseInt(v, 10); }
	public static int toInt(String v, int radix){ return Integer.parseInt(v, radix); }
	public static int toInt(Long v){ return v.intValue(); }
	
	public static short toShort(Object obj){ 
		if(obj == null){ 		
			throw new NullPointerException("convert null to short");
		}
		if(obj instanceof Number){
			return ((Number)obj).shortValue();
		}
		if(obj instanceof Character){
			return toShort((Character)obj);
		}else if(obj instanceof Boolean){
			return toShort((Boolean)obj);
		}else if(obj instanceof String){
			return toShort((String)obj);
		}
		throw new ClassCastException(obj+" 不能转换为 short 类型");
	}
	public static short toShort(Boolean v){ return (short)(v.booleanValue()?1:0); }
	public static short toShort(Character v){ return (short)v.charValue(); }
	public static short toShort(Byte v){ return v.shortValue(); }
	public static short toShort(Integer v){ return v.shortValue(); }
	public static short toShort(Float v){ return v.shortValue(); }
	public static short toShort(Short v){ return v.shortValue(); }
	public static short toShort(Double v){ return v.shortValue(); }
	public static short toShort(String v){ return Short.parseShort(v, 10); }
	public static short toShort(String v, int radix){ return Short.parseShort(v, radix); }
	public static short toShort(Long v){ return v.shortValue(); }
	
	public static float toFloat(Object obj){
		if(obj == null){
			throw new NullPointerException("convert null to float");
		}
		if(obj instanceof Number){
			return ((Number)obj).floatValue();
		}
		if(obj instanceof Character){
			return toFloat((Character)obj);
		}else if(obj instanceof Boolean){
			return toFloat((Boolean)obj);
		}else if(obj instanceof String){
			return toFloat((String)obj);
		}
		throw new ClassCastException(obj+" 不能转换为  float 类型");
	}
	public static float toFloat(Boolean v){ return (float)(v.booleanValue()?1:0); }
	public static float toFloat(Character v){ return (float)v.charValue(); }
	public static float toFloat(Byte v){ return v.floatValue(); }
	public static float toFloat(Integer v){ return v.floatValue(); }
	public static float toFloat(Float v){ return v.floatValue(); }
	public static float toFloat(Short v){ return v.floatValue(); }
	public static float toFloat(Double v){ return v.floatValue(); }
	public static float toFloat(String v){ return Float.parseFloat(v); }
	public static float toFloat(Long v){ return v.floatValue(); }
	
	public static long toLong(Object obj){
		if(obj == null){
			throw new NullPointerException("convert null to long");
		}
		if(obj instanceof Number){
			return ((Number)obj).longValue();
		}
		if(obj instanceof Character){
			return toLong((Character)obj);
		}else if(obj instanceof Boolean){
			return toLong((Boolean)obj);
		}else if(obj instanceof String){
			return toLong((String)obj);
		}
		throw new ClassCastException(obj+" 不能转换为  long 类型");
	}
	public static long toLong(Boolean v){ return (long)(v.booleanValue()?1:0); }
	public static long toLong(Character v){ return (long)v.charValue(); }
	public static long toLong(Byte v){ return v.longValue(); }
	public static long toLong(Integer v){ return v.longValue(); }
	public static long toLong(Float v){ return v.longValue(); }
	public static long toLong(Short v){ return v.longValue(); }
	public static long toLong(Double v){ return v.longValue(); }
	public static long toLong(String v){ return Long.parseLong(v, 10); }
	public static long toLong(String v, int radix){ return Long.parseLong(v, radix); }
	public static long toLong(Long v){ return v.longValue(); }
	
	public static double toDouble(Object obj){ 
		if(obj == null){ 		
			throw new NullPointerException("convert null to double");
		}
		if(obj instanceof Number){
			return ((Number)obj).doubleValue();
		}
		if(obj instanceof Character){
			return toDouble((Character)obj);
		}else if(obj instanceof Boolean){
			return toDouble((Boolean)obj);
		}else if(obj instanceof String){
			return toDouble((String)obj);
		}
		throw new ClassCastException(obj+" 不能转换为  double 类型");
	}
	public static double toDouble(Boolean v){ return (double)(v.booleanValue()?1:0); }
	public static double toDouble(Character v){ return (double)v.charValue(); }
	public static double toDouble(Byte v){ return v.doubleValue(); }
	public static double toDouble(Integer v){ return v.doubleValue(); }
	public static double toDouble(Float v){ return v.doubleValue(); }
	public static double toDouble(Short v){ return v.doubleValue(); }
	public static double toDouble(Double v){ return v.doubleValue(); }
	public static double toDouble(String v){ return Double.parseDouble(v); }
	public static double toDouble(Long v){ return v.doubleValue(); }
	
	public static byte toByte(Object obj){ 
		if(obj == null){ 		
			throw new NullPointerException("convert null to byte");
		}
		if(obj instanceof Number){
			return ((Number)obj).byteValue();
		}
		if(obj instanceof Character){
			return toByte((Character)obj);
		}else if(obj instanceof Boolean){
			return toByte((Boolean)obj);
		}else if(obj instanceof String){
			return toByte((String)obj);
		}
		throw new ClassCastException(obj+" 不能转换为  byte 类型");
	}
	public static byte toByte(Boolean v){ return (byte)(v.booleanValue()?1:0); }
	public static byte toByte(Character v){ return (byte)v.charValue(); }
	public static byte toByte(Byte v){ return v.byteValue(); }
	public static byte toByte(Integer v){ return v.byteValue(); }
	public static byte toByte(Float v){ return v.byteValue(); }
	public static byte toByte(Short v){ return v.byteValue(); }
	public static byte toByte(Double v){ return v.byteValue(); }
	public static byte toByte(String v){ return Byte.parseByte(v, 10); }
	public static byte toByte(String v, int radix){ return Byte.parseByte(v, radix); }
	public static byte toByte(Long v){ return v.byteValue(); }
	
	public static char toChar(Object obj){ 
		if(obj == null){ 		
			throw new NullPointerException("convert null to char");
		}
		if(obj instanceof Integer){
			return toChar((Integer)obj);
		}else if(obj instanceof Long){
			return toChar((Long)obj);
		}else if(obj instanceof Short){
			return toChar((Short)obj);
		}else if(obj instanceof Character){
			return toChar((Character)obj);
		}else if(obj instanceof Byte){
			return toChar((Byte)obj);
		}else if(obj instanceof String){
			if(((String) obj).length()==1){
				return ((String) obj).charAt(0);
			}
		}
		throw new ClassCastException(obj+" 不能转换为  char 类型");
	}
	public static char toChar(Character v){ return (char)v.charValue(); }
	public static char toChar(Byte v){ return (char)v.intValue(); }
	public static char toChar(Integer v){ return (char)v.intValue(); }
	public static char toChar(Short v){ return (char)v.intValue(); }
	public static char toChar(Long v){ return (char)v.intValue(); }
	
	public static boolean toBoolean(Object obj){ 
		if(obj == null){ 		
			throw new NullPointerException("convert null to boolean");
		}
		if(obj instanceof Integer){
			return toBoolean((Integer)obj);
		}else if(obj instanceof Long){
			return toBoolean((Long)obj);
		}else if(obj instanceof Short){
			return toBoolean((Short)obj);
		}else if(obj instanceof String){
			return toBoolean((String)obj);
		}else if(obj instanceof Character){
			return toBoolean((Character)obj);
		}else if(obj instanceof Boolean){
			return toBoolean((Boolean)obj);
		}else if(obj instanceof Byte){
			return toBoolean((Byte)obj);
		}
		throw new ClassCastException(obj+" 不能转换为  boolean 类型");
	}
	public static boolean toBoolean(Boolean v){ return v.booleanValue(); }
	public static boolean toBoolean(Character v){ return v=='T'||v=='t'||v=='1'||v=='Y'||v=='y'; }
	public static boolean toBoolean(Byte v){ return v.byteValue()!=0; }
	public static boolean toBoolean(Integer v){ return v.intValue()!=0; }
	public static boolean toBoolean(Short v){ return v.shortValue()!=0; }
	public static boolean toBoolean(String v){ return v.equals("true")||v.equals("yes")||v.equals("1"); }
	public static boolean toBoolean(Long v){ return v.longValue()!=0; }
}